<?php

namespace app\api\controller;

use app\common\model;

// 任务系统用户控制器
class TaskUser extends TaskBase
{

    /**
     * 获取用户账号列表
     */
    public function accountList () {
        $page = $this->request->param("page",1);
        $pageSize = $this->request->param("pageSize", 15);
        $user = $this->auth->getUser();
        $query = model\User\Account::where('user_id', $user->id);
        $total = $query->count();
        $accounts = $query
            ->order('id','desc')
            ->page($page, $pageSize)
            ->select()
            ->toArray();

        foreach ($accounts as &$account) {
            $account['create_time_text'] = $account['createtime'] ? date('Y-m-d H:i:s', $account['createtime']) : '';
            $account['update_time_text'] = $account['updatetime'] ? date('Y-m-d H:i:s', $account['updatetime']) : '';
        }
        unset($account);

        return json([
            "status" =>1, "msg" =>"获取成功",
            "data" => [
                "list" => $accounts,
                "total" => $total,
                "page" => $page,
                "pageSize" => $pageSize
            ]
        ]);
    }

    /**
     * 用户平台注册
     */
    public function register () {
        if (!$this->request->isPost()) {
            return json(["status" => 0,"msg" =>"错误的请求"]);
        }
        $user = $this->auth->getUser();

        $platform = $this->request->post('platform');
        $account_id = $this->request->post('account_id','', 'trim');
        $account_name = $this->request->post('account_name','', 'trim');
        $personal_url = $this->request->post('personal_url','', 'trim');
        $status = $this->request->post('status', 1);
        $fans_num = $this->request->post('fans_num', 0);

        if (empty($platform) || !in_array($platform, array_keys(model\User\Account::PLATFORM))) {
            return json(["status" => 0,"msg" =>"参数错误"]);
        }
        if (empty($account_id) || empty($account_name) || empty($personal_url)) {
            return json(["status" => 0,"msg" =>"参数错误"]);
        }
        if (!in_array($status, [0,1,2]) || !is_numeric($fans_num)) {
            return json(["status" => 0,"msg" =>"参数错误"]);
        }

        model\User\Account::create([
            'user_id' => $user->id,
            'platform' => $platform,
            'account_id' => $account_id,
            'account_name' => $account_name,
            'personal_url' => $personal_url,
            'status' => $status,
            'fans_num' => $fans_num,
        ]);

        return json(["status" => 1,"msg" =>"操作成功"]);
    }

    /**
     * 是否使用
     */
    public function statusSwitch () {
        if (!$this->request->isPost()) {
            return json(["status" => 0,"msg" =>"错误的请求"]);
        }
        $user = $this->auth->getUser();
        $id = $this->request->post('id',0);
        if (empty($id)) {
            return json(["status" => 0,"msg" =>"参数错误"]);
        }
        $account = model\User\Account::where('user_id', $user->id)->where('id', $id)->find();
        if (!$account) {
            return json(["status" => 0,"msg" =>"账户不存在"]);
        }
        $status = $account->status;
        $account->status = $status == 2 ? 1 : 2;
        $account->save();
        return json(["status" => 1,"msg" =>"操作成功"]);
    }

    /**
     * 我的任务
     */
    public function taskList() {
        $page = $this->request->params('page', 1);
        $pageSize = $this->request->param('pageSize', 15);
        $status = $this->request->param('status', 0);
        $user = $this->auth->getUser();
        $query = model\Pick::where('user_id', $user->id)->where('status', $status);
        $total = $query->count();
        $taskLists = $query->order('id', 'desc')
            ->page($page, $pageSize)
            ->select()
            ->toArray();

        return json([
            "status" =>1, "msg" =>"获取成功",
            "data" => [
                "list" => $taskLists,
                "total" => $total,
                "page" => $page,
                "pageSize" => $pageSize
            ]
        ]);
    }

    /**
     * 接取任务
     */
    public function taskPick () {
        if (!$this->request->isPost()) {
            return json(["status" => 0,"msg" =>"错误的请求"]);
        }

        $assignment_id = $this->request->post('assignment_id', 0);
        if (empty($assignment_id)) {
            return json(["status" => 0,"msg" =>"参数错误"]);
        }

        $now = time();

        $assignment = model\Assignment::where('start_time','<=', $now)
            ->where('end_time','>', $now)
            ->where('id', $assignment_id)
            ->find();

        if (!$assignment) {
            return json(["status" => 0,"msg" =>"无效任务"]);
        }

        $user = $this->auth->getUser();
        $count = model\Pick::where('user_id', $user->id)->where('assignment_id', $assignment_id)->count();
        if ($count == 0) {
            return json(["status" => 0,"msg" =>"该任务已接取"]);
        }

        model\Pick::create([
            'user_id' => $user->id,
            'assignment_id' => $assignment_id,
            'name' => $assignment['name'],
            'access_time' => $now,
            'end_time' => $assignment['end_time']
        ]);
        return json(["status" => 1,"msg" =>"操作成功"]);
    }

    /**
     * 接取任务列表
     */
    public function pickTaskList () {
        $page = $this->request->params('page', 1);
        $pageSize = $this->request->param('pageSize', 15);

        $pick_id = $this->request->param('pick_id', 0);
        $status = $this->request->param('status', 0);

        $query = model\pick\Settlement::where('pick_id', $pick_id)->where('status', $status);
        $total = $query->count();
        $pickTaskLists = $query->order('id', 'desc')->page($page, $pageSize)->select()->toArray();


        return json([
            "status" =>1, "msg" =>"获取成功",
            "data" => [
                "list" => $pickTaskLists,
                "total" => $total,
                "page" => $page,
                "pageSize" => $pageSize
            ]
        ]);
    }
}